//
//  TopicsViewController.m
//  Koala_Fm
//
//  Created by dllo on 16/2/25.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "TopicsViewController.h"
#import "TopicsTableViewCell.h"
#import "AFNetBlock.h"
#import "TopicsModel.h"
#import <UIImageView+WebCache.h>
#import "PdViewController.h"
@interface TopicsViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *dataArr;

@end

@implementation TopicsViewController
- (void)dealloc
{
    [_tableView release];
    [_dataArr release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"热门专题";
    /** tableView设置 **/
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
//    _tableView.backgroundColor = [UIColor redColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView release];
    
    // 重用池
    [_tableView registerClass:[TopicsTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    /** 数据获取 **/
    NSString *str = @"http://m.kaolafm.com/apiv4/topic/list?pagenum=1&pagesize=10";
    [AFNetBlock GetDataWithURL:str block:^(id block) {
        NSArray *array = block[@"result"][@"dataList"];
        self.dataArr = [TopicsModel modelHandlerWithArray:array];
        //   NSLog(@"%@", _dateArr);
        
        // 刷新页面
        
        [_tableView reloadData];
        
    }];

    }
#pragma mark - tableView协议设置
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
     TopicsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    TopicsModel *m = _dataArr[indexPath.row];
    
    [cell.image sd_setImageWithURL:[NSURL URLWithString:m.img]];
    cell.lable.text = m.name;
    
    
    return cell;
}
// 高度设置
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 210;
}
// 点击cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    // 取消选中状态
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    PdViewController *pd = [[PdViewController alloc]init];
    // 传值给下一页
    pd.model = _dataArr[indexPath.row];
    
    [self.navigationController pushViewController:pd animated:YES];


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
